layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
        gid=3;
    //学生预报名审核列表
    var tableIns = table.render({
		  elem: '#admin',
		  url : 'http://127.0.0.1:8000/rolelist?gid='+gid,
		  cellMinWidth : 95,
		  page : true,
		  //toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
		  defaultToolbar: ['filter', 'exports', 'print'],
		  limit : 20,
		  limits : [10,15,20,25],
		  id : "admin",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:60, align:"center" ,fixed:"left"},
            {field: 'username', title: '工号', width:130},
            {field: 'realname', title: '姓名', width:130},
            {field: 'sex', title: '性别',  align:'center',width:130},
            {field: 'phone', title: '手机号', align:'center',width: 130},
            {field: 'email', title: '邮箱', align:'center',width: 180},
            {field: 'role', title: '角色权限', align:'center',width: 180},
           
            {title: '操作', width:215, templet:'#examineBar',fixed:"right",align:"center"}
        ]]
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].newsId);
            }
            layer.confirm('确定删除选中的文章？', {icon: 3, title: '提示信息'}, function (index) {
                // $.get("删除文章接口",{
                //     newsId : newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                tableIns.reload();
                layer.close(index);
                // })
            })
        }else{
            layer.msg("请选择需要删除的文章");
        }
    })

    //详情
    function adminDetail(edit){
        var index = layui.layer.open({
            title : "用户详情",
            type : 2,
			area: ['90%', '60%'],
			fixed: false, //不固定
			maxmin: true,
            content : "adminDetail.html",
            // content : "../public/changePwd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".joNnumber").val(edit.username);
                    body.find(".name").val(edit.realname);
                    // body.find(".sex").attr("src",edit.sex);
                    body.find(".userPhone").val(edit.phone);
                    // body.find(".Accountability").val(edit.Accountability);
                    body.find(".userEmail").val(edit.email);
                    // body.find(".myself").val(edit.myself);
                    // body.find(".openness input[name='openness'][title='"+edit.newsLook+"']").prop("checked","checked");
                    // body.find(".newsTop input[name='newsTop']").prop("checked",edit.newsTop);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
    }

    //密码修改
    function passwdUpdate(edit){
        var index = layui.layer.open({
            title : "密码修改",
            type : 2,
			area: ['90%', '45%'],
			fixed: false, //不固定
			maxmin: true,
            // content : "adminDetail.html",
            content : "../public/changePwd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".id").val(edit);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
    }
   //权限修改
  function powerUpdate(edit){
    var index = layui.layer.open({
        title : "权限修改",
        type : 2,
        area: ['90%', '35%'],
        fixed: false, //不固定
        maxmin: true,
        content : "../public/power.html",
        success : function(layero, index){
            var body = layui.layer.getChildFrame('body', index);
            
                console.log(edit)
                power='老师'
                body.find(".id").val(edit.id);
                body.find("input[name='power'][value='" + power + "']").prop("checked", true);
                form.render();
            
            setTimeout(function(){
                layui.layer.tips('返回', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            },500)
        }
    })
}
     //列表操作
     table.on('tool(admin)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
			console.log(data)
            localStorage.setItem("ChoiceID",data.username)
            localStorage.setItem("ChoicePSW",data.password)
        var powerData = {id:data.id,power:data.power};
        if(layEvent === 'detail'){ //编辑
            adminDetail(data);
        }
        else if(layEvent === 'passwd'){
            passwdUpdate(data.id);
        } 
        else if(layEvent === 'power'){
            powerUpdate(powerData);
        }
        // else if(layEvent === 'del'){ //删除
            // layer.confirm('确定删除此文章？',{icon:3, title:'提示信息'},function(index){
            //     // $.get("删除文章接口",{
            //     //     newsId : data.newsId  //将需要删除的newsId作为参数传入
            //     // },function(data){
            //         tableIns.reload();
            //         layer.close(index);
            //     // })
            // });
        // } 
    });

})